from django.db import models
from utils.models import BaseModel


class Category(BaseModel):
    cname = models.CharField(max_length=10, verbose_name='种类')

    class Meta:
        verbose_name_plural = '种类'
        verbose_name = verbose_name_plural

    def __str__(self):
        return self.cname


class Brand(BaseModel):
    brand = models.CharField(max_length=10, verbose_name="品牌")

    class Meta:
        verbose_name_plural = '品牌'
        verbose_name = verbose_name_plural

    def __str__(self):
        return self.brand


class DetailName(BaseModel):
    gd_name = models.CharField(max_length=30, verbose_name='详细细节')

    def __str__(self):
        return self.gd_name

    class Meta:
        verbose_name_plural = '详细细节'
        verbose_name = verbose_name_plural


class Size(BaseModel):
    s_name = models.CharField(max_length=10, verbose_name='尺寸')

    def __str__(self):
        return self.s_name

    class Meta:
        verbose_name_plural = '尺寸'
        verbose_name = verbose_name_plural


class Color(BaseModel):
    color_name = models.CharField(max_length=10, verbose_name='颜色')

    def __str__(self):
        return self.color_name

    class Meta:
        verbose_name_plural = '颜色'
        verbose_name = verbose_name_plural


class Goods(BaseModel):
    g_name = models.CharField(max_length=100, verbose_name='商品名称')
    desc = models.CharField(max_length=100, verbose_name='商品描述')
    old_price = models.DecimalField(max_digits=5, decimal_places=2, verbose_name='原价')
    price = models.DecimalField(max_digits=5, decimal_places=2, verbose_name='现价')
    category = models.ForeignKey(Category, models.CASCADE, verbose_name='种类')
    color = models.ManyToManyField(to="Color")
    # 与尺码建立多对多关系
    size = models.ManyToManyField(to="Size")

    class Meta:
        verbose_name_plural = '商品'
        verbose_name = verbose_name_plural

    def __str__(self):
        return self.g_name


class Inventory(BaseModel):
    counts = models.PositiveIntegerField(verbose_name='库存数量')
    goods = models.ForeignKey(Goods, models.CASCADE, verbose_name='商品')

    def __str__(self):
        return '{}-{}'.format(self.goods.g_name, self.counts)

    class Meta:
        verbose_name_plural = '库存'
        verbose_name = verbose_name_plural


class Detail(BaseModel):
    gd_url = models.ImageField(upload_to='detail')
    gd_name = models.ForeignKey(DetailName, models.CASCADE, verbose_name='详细')
    goods = models.ForeignKey(Goods, models.CASCADE, verbose_name='商品')

    def __str__(self):
        return self.goods.g_name

    class Meta:
        verbose_name_plural = '细节'
        verbose_name = verbose_name_plural

    # 获取详情名称
    def name(self):
        return self.gd_name.gd_name

